import gapiclient
import oauth
import re
import lookup
        
class scheduler:
    def __init__(self):
            self.http = oauth.authorize()
            self.con = gapiclient.build('spreadsheets','v3',http=self.http)
            self.classList = []
        
    def getRows(self):
        #connects to google API service using the access key
        content = self.con.row().list(ssid='0Au42S2qVLNisdHlpMUNYQU5WME1DQ21lR1cyZFVITWc',wsid='od6')
        
        rows = content.execute()['entry'] #gets the rows feed

        for row in rows:
            self.classList.append(row['values'])
        return self.classList
    
    def isClassSame(self,class1,class2):
        if (class1 == class2): return True
        else:
            if class1['fixedcredit'] != class2['fixedcredit']: return False
            if daysToMeet(class1['daysheld']) != daysToMeet(class2['daysheld']): return false
            if class1['starttime'] != class2['starttime']: return False
            if class1['stoptime'] != class2['stoptime']: return False
            
            if class1['subj'] != class2['subj']: return False
            if class1['crse'] != class2['crse']: return False
            if class1['title'] != class2['title']: return False
        return True
            



    def getAll(self,value,field = "crse"):
        dictionary = {}
        for eachList in self.classList:
            if eachList[field] == value:
                dictionary[self.classList.index(eachList)] = eachList
        return dictionary
                
            

  
def daysToMeet(s):
    #function must be called with abbreviation as follows
    #MWTRF: M - Monday, T - Tuesday, W - Wednesday,  R - Thursday, F - Friday
    #If it contains space it is treated as different day
    #Example: "MWT RF" the class meets at one time on MWT and different time on RF
    days = []
    allDays = re.split('\W',s.strip()) #if there's space between days then it regards as meetings at separate time
    for eachSetOfMeetings in allDays:
        if (len(eachSetOfMeetings) != 0):
            listOfDay = []
            for eachMeetings in eachSetOfMeetings:
                listOfDay.append(eachMeetings)
            days.append(listOfDay)
    return days #return type is list of lists where each element in list is a meeting at a particular time

def getFacultyEmail(s):
    return lookup.search(s)
